<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>引出生命周期</title>
    <script src="../js/vue.js"></script>
</head>
<body>
<div id="root">
    <h1 :style="{opacity}">你好，我是彭于晏</h1>
    <button @click="opacity=1">点我透明度恢复为1</button>
    <button @click="des">点我停止闪烁</button>
</div>
<script>
    Vue.config.productionTip = false;
    const vm = new Vue({
        el: '#root',
        data: {
            opacity:1
        },
        methods: {
          des() {
              //注意 销毁vm方法之后 定时器没有停止
              this.$destroy();
          }
        },
        //初次解析完模板之后调用此方法 （这就是生命周期方法）里面的this指向 vm
        mounted() {
            this.timer = setInterval(() => {
                console.log(1)
                if (this.opacity <= 0) {
                    this.opacity = 1;
                }
                this.opacity -= 0.01;
            },16)
        },
        beforeDestroy() {
            clearInterval(this.timer);
        }
    });
</script>
</body>
</html>